﻿//==============================================================================
// File: RuleList.cs
// Created: 2010-05-10
// Author: Piotr Włodek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with WpfRcp. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Collections;
using System.Collections.Generic;

namespace WpfRcp.PropertyModel.Validation
{
    /// <summary>
    /// Represents a collection of validation rules.
    /// </summary>
    [Serializable]
    public class RuleList : IEnumerable<IValidationRule>, IRuleListChanged
    {
        private List<IValidationRule> Rules { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="RuleList"/> class.
        /// </summary>
        public RuleList()
        {
            Rules = new List<IValidationRule>();
        }

        /// <summary>
        /// Adds new validation rule.
        /// </summary>
        /// <param name="rule">Validation rule to be added.</param>
        public void Add(IValidationRule rule)
        {
            if (rule == null)
                throw new ArgumentNullException("rule");

            Rules.Add(rule);
            OnRuleListChanged(rule);
        }

        /// <summary>
        /// Adds new validation callback.
        /// </summary>
        /// <param name="callback">Validation callbac to be added.</param>
        public void Add(ValidateValueCallback callback)
        {
            if (callback == null)
                throw new ArgumentNullException("callback");

            Add(new DelegateValidationRule(callback));
        }

        #region IEnumerable<IValidationRule> Implementation

        IEnumerator<IValidationRule> IEnumerable<IValidationRule>.GetEnumerator()
        {
            return Rules.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return Rules.GetEnumerator();
        }

        #endregion

        #region IRuleListChanged Implementation

        /// <summary>
        /// Event fired when a rule list changes.
        /// </summary>
        [field: NonSerialized]
        public event EventHandler<RuleListChangedEventArgs> RuleListChanged;

        protected virtual void OnRuleListChanged(IValidationRule newRule)
        {
            if (RuleListChanged != null)
                RuleListChanged(this, new RuleListChangedEventArgs(newRule));
        }

        #endregion
    }
}